//
//  LFHeaderView.m
//  LoveFruit
//
//  Created by 马海江 on 16/9/24.
//  Copyright © 2016年 张令林. All rights reserved.
//

#import "LFHeaderView.h"
#import "NSAttributedString+CZAdditon.h"

@implementation LFHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}
- (void)setupUI{
    // 添加Button
    [self addButtons:@"v2_my_order_icon" title:@"我的订单" tag:1];
    [self addButtons:@"v2_my_coupon_icon" title:@"优惠券" tag:2];
    [self addButtons:@"v2_my_message_icon" title:@"我的消息" tag:3];
}
// 添加按钮
- (void)addButtons:(NSString *)imageName title:(NSString *)title tag:(NSInteger)tag{
    UIButton *button = [[UIButton alloc]init];
    button.tag = tag;
    NSAttributedString *str = [NSAttributedString cz_imageTextWithImage:[UIImage imageNamed:imageName] imageWH:30 title:title fontSize:12 titleColor:[UIColor darkGrayColor] spacing:7];
    [button setAttributedTitle:str forState:UIControlStateNormal];
    //自动换行
    button.titleLabel.numberOfLines = 0;
    button.titleLabel.textAlignment = NSTextAlignmentCenter;
    [button sizeToFit];
    
    [self addSubview:button];
    [button addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
}
// 按钮的点击事件
- (void)clickBtn:(UIButton *)sender{
    switch (sender.tag) {
        case 1:
            NSLog(@"第一个按钮点击了");
            break;
        case 2:
            NSLog(@"第二个按钮点击了");
            break;
        case 3:
            NSLog(@"第三个按钮点击了");
            break;
        default:
            break;
    }
}
- (void)layoutSubviews{
    
    CGFloat margin = (self.frame.size.width - self.subviews[0].frame.size.width * 3)/4;
    //需要把个数减1
    for (NSInteger i = 0; i < self.subviews.count - 1; i++) {
        //拿到当前按钮
        UIButton *button = self.subviews[i];
        //拿到下一个按钮
        UIButton *nextButton = self.subviews[i+1];
        if (i == 0) {
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.centerY.equalTo(self);
                make.left.equalTo(self).offset(margin);
            }];
        }
        [nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(button.mas_right).offset(margin);
            make.centerY.equalTo(self);
        }];
    }
}
@end
